package com.example.test84;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHeaper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHeaper = new MyDatabaseHelper(this,"BookStore2.db",null,2);
        SQLiteDatabase db = dbHeaper.getWritableDatabase();
//        ContentValues values = new ContentValues();
//
//
//        values.put("category_name","经济类");
//        values.put("category_code",1);
//        //插入第一条数据
//        db.insert("Category",null,values);
//        values.clear();
//
//        values.put("category_name","计算机类");
//        values.put("category_code",2);
//        //插入第二条数据
//        db.insert("Category",null,values);
//        values.clear();
//
//
//        values.put("name","JS");
//        values.put("author","Lisa");
//        values.put("pages",12);
//        values.put("price",32);
//        values.put("category_id",2);
//        db.insert("Book",null,values);
//        values.clear();
    }

    public List<Book> selectBook(){
        List<Book> books = new ArrayList<Book>();
        dbHeaper = new MyDatabaseHelper(this,"BookStore2.db",null,2);
        SQLiteDatabase db = dbHeaper.getWritableDatabase();
//      ContentValues values = new ContentValues();
        //查询表中的数据
        Cursor cursor = db.query("Book,Category", new String[]{"Book.*","Category.category_name","Category.category_code"},
                "Book.category_id = Category.c_id ",null,null,null,null);
        if (cursor.moveToFirst()){
            do{
                //遍历Cursor对象，取出数据
                Book book = new Book();
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
                @SuppressLint("Range") String author = cursor.getString(cursor.getColumnIndex("author"));
                @SuppressLint("Range") int price = cursor.getInt(cursor.getColumnIndex("price"));
                @SuppressLint("Range") int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
                @SuppressLint("Range") int category_id = cursor.getInt(cursor.getColumnIndex("category_id"));
                @SuppressLint("Range") int category_code = cursor.getInt(cursor.getColumnIndex("category_code"));
                @SuppressLint("Range") String category_name = cursor.getString(cursor.getColumnIndex("category_name"));

                book.setId(id);
                book.setAuthor(author);
                book.setPages(pages);
                book.setName(name);
                book.setPrice(price);
                book.setCategory_id(category_id);
                book.setCategroy_code(category_code);
                book.setCategory_name(category_name);

                books.add(book);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return books;
    }

    @Override
    protected void onResume() {

        setContentView(R.layout.activity_main);

        super.onResume();

        List<Book> books = selectBook();


        ListView bookListView = (ListView) findViewById(R.id.book_listView);
        bookListView.setAdapter(new BookAdapter(books,this));


        Button addBook = (Button) findViewById(R.id.addBook);
        addBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AddBookActivity.class);
                startActivity(intent);
            }
        });

    }
}